package com.example.hyznbk.view.fragment;

import android.os.Bundle;
import android.support.annotation.Nullable;
import android.text.Html;
import android.view.View;
import android.view.ViewGroup;
import android.widget.LinearLayout;
import android.widget.PopupWindow;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.example.hyznbk.model.result.BaseReviewItemEntity;
import com.example.hyznbk.presenter.GuessTestingPresenter;
import com.example.hyznbk.R;
import com.example.hyznbk.view.adapter.GuessSubjectAdapter;
import com.example.hyznbk.view.adapter.PopupMenuType1Adapter;
import com.example.hyznbk.view.base.BaseMainFragment;
import com.example.hyznbk.view.base.ILoadMoreDataView;
import com.example.hyznbk.view.widget.CustomFooterView;
import com.example.hyznbk.view.widget.PopupMenu;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by Administrator on 2017/2/6.
 * 猜题测试
 */
public class GuessTestingFragment extends BaseMainFragment<GuessTestingPresenter> implements View.OnClickListener,ILoadMoreDataView<BaseReviewItemEntity>{



    private GuessSubjectAdapter mGuessSubjectAdapter;

    private TextView mTvSpStatus;
    private TextView mTvSpPaperFrom;
    private TextView mTvTotal;
    private PopupMenu mPaperFromPopupMenu;
    private PopupMenu mQuesStatusPopupMenu;
    private View mMasker;
    private RelativeLayout mSpLayout;
    //private Toolbar mToolbar;

    private Map<String,Integer> mQuesStatusMap = new HashMap<>();
    private Map<String,String> mPaperFromMap = new HashMap<>();

    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

    }

    @Override
    protected void updateViews(boolean isShowLoading) {
        //setHasOptionsMenu(false);
        mPresenter.getData(isShowLoading);
    }

    @Override
    public int attachLayoutRes() {
        return R.layout.fragment_guess_testing;
    }

    @Override
    public void initBaseMainViews(View view, Bundle savedInstanceState) {
        mPresenter = new GuessTestingPresenter(this);

        mSpLayout = (RelativeLayout) view.findViewById(R.id.rel_toolbar_head);
        mTvSpStatus = (TextView) view.findViewById(R.id.sp_status);
        mTvSpPaperFrom = (TextView) view.findViewById(R.id.sp_paperFrom);
        mTvTotal = (TextView) view.findViewById(R.id.tv_paperNumber);
        mMasker = view.findViewById(R.id.bg_masker);

        View toolBarLayout = view.findViewById(R.id.rel_toolbar_layout);
        ((TextView)toolBarLayout.findViewById(R.id.tv_toolbar_title)).setText(R.string.guessTesting);
        toolBarLayout.findViewById(R.id.img_main_menu).setVisibility(View.GONE);

        ViewGroup.MarginLayoutParams layoutParams = (ViewGroup.MarginLayoutParams) toolBarLayout.getLayoutParams();
        layoutParams.setMargins(0,getStatusBarHeight(),0,0);

       /* mToolbar = (Toolbar) view.findViewById(R.id.toolbar);
        initToolBar(mToolbar,false,R.string.guessTesting);*/
        mTvSpStatus.setOnClickListener(this);

        mTvSpPaperFrom.setOnClickListener(this);


        String[] quesStatusArrays = getResources().getStringArray(R.array.QuesStatusArrays);
        String[] quesStatusName = new String[quesStatusArrays.length];
        for (int i = 0 ; i < quesStatusArrays.length ; i++){
            String[] split = quesStatusArrays[i].split("\\|");
            quesStatusName[i] = split[0];
            mQuesStatusMap.put(split[0], Integer.valueOf(split[1]));
        }

        PopupMenuType1Adapter mQuesStatusAdapter = new PopupMenuType1Adapter(getHolderActivity(), quesStatusName);
        mQuesStatusPopupMenu = new PopupMenu(getHolderActivity(), LinearLayout.LayoutParams.MATCH_PARENT,R.drawable.bg_popupmenu);
        mQuesStatusPopupMenu.setListAdapter(mQuesStatusAdapter);
        mQuesStatusPopupMenu.setOnItemClickListener(new PopupMenu.OnItemClickListener() {
            @Override
            public void onItemClick(View item, int position, long id) {
                mTvSpStatus.setText(mQuesStatusPopupMenu.getCheckedStr());
                mXRefreshView.startRefresh();
                mPresenter.getDataByStatus(mQuesStatusMap.get(mQuesStatusPopupMenu.getCheckedStr()));
            }
        });
        mTvSpStatus.setText(mQuesStatusPopupMenu.getCheckedStr());
        mQuesStatusPopupMenu.setOnDismissListener(new PopupWindow.OnDismissListener() {
            @Override
            public void onDismiss() {
                mMasker.setVisibility(View.GONE);
                mTvSpStatus.setSelected(false);
            }
        });

        String[] paperFromArrays = getResources().getStringArray(R.array.PaperFromArrays);
        String[] paperFromName = new String[paperFromArrays.length];
        for (int i = 0 ; i < paperFromArrays.length ; i++){
            String[] split = paperFromArrays[i].split("\\|");
            paperFromName[i] = split[0];
            mPaperFromMap.put(split[0], split[1]);
        }

        PopupMenuType1Adapter mPaperFromAdapter = new PopupMenuType1Adapter(getHolderActivity(), paperFromName);
        mPaperFromPopupMenu = new PopupMenu(getHolderActivity(), LinearLayout.LayoutParams.MATCH_PARENT,R.drawable.bg_popupmenu);
        mPaperFromPopupMenu.setListAdapter(mPaperFromAdapter);
        mPaperFromPopupMenu.setOnItemClickListener(new PopupMenu.OnItemClickListener() {
            @Override
            public void onItemClick(View item, int position, long id) {
                mTvSpPaperFrom.setText(mPaperFromPopupMenu.getCheckedStr());
                mXRefreshView.startRefresh();
                mPresenter.getDataByPaperFrom(mPaperFromMap.get(mPaperFromPopupMenu.getCheckedStr()));
            }
        });
        mTvSpPaperFrom.setText(mPaperFromPopupMenu.getCheckedStr());
        mPaperFromPopupMenu.setOnDismissListener(new PopupWindow.OnDismissListener() {
            @Override
            public void onDismiss() {
                mMasker.setVisibility(View.GONE);
                mTvSpPaperFrom.setSelected(false);
            }
        });


        mGuessSubjectAdapter = new GuessSubjectAdapter(getActivity());
        mGuessSubjectAdapter.setCustomLoadMoreView(new CustomFooterView(getActivity()));
        mRecyclerView.setAdapter(mGuessSubjectAdapter);
        mGuessSubjectAdapter.setOnItemClickListener(new GuessSubjectAdapter.OnItemClickListener() {
            @Override
            public void onItemClick(View view, List<BaseReviewItemEntity.BaseReviewItemBean> list, int position) {
                mPresenter.openItemDetail(position,list.get(position));
            }
        });
    }


    @Override
    public void showNetError() {
        super.showNetError();
        setPaperTotal(0);
    }



    @Override
    public void showNoData(String msg) {
        super.showNoData(msg);
        setPaperTotal(0);
    }



    @Override
    public void loadData(BaseReviewItemEntity data) {
        if(mRecyclerView.getLayoutManager() != null ) {
            mRecyclerView.getViewTreeObserver().addOnGlobalLayoutListener(layoutListener);
        }
        setPaperTotal(data.getTotalCount());
        mGuessSubjectAdapter.clearAndAdd(data.getModelList());
        loadComplete(mGuessSubjectAdapter.getAdapterItemCount() >= data.getTotalCount());
    }

    @Override
    public void loadMoreData(BaseReviewItemEntity data) {
        setPaperTotal(data.getTotalCount());
        mGuessSubjectAdapter.add(data.getModelList());
        loadComplete(mGuessSubjectAdapter.getAdapterItemCount() >= data.getTotalCount());
    }




    /**
     * 设置试卷总数
     */
    private void setPaperTotal(int number) {
        String paperTotal = String.format(getString(R.string.testingTotal),number);
        mTvTotal.setText(Html.fromHtml(paperTotal));
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()){
           case R.id.sp_status:
               mMasker.setVisibility(View.VISIBLE);//阴影布局可见
               mTvSpStatus.setSelected(true);
               mQuesStatusPopupMenu.showLocation(mSpLayout, 0, 0);
               break;
            case R.id.sp_paperFrom:
                mMasker.setVisibility(View.VISIBLE);//阴影布局可见
                mTvSpPaperFrom.setSelected(true);
                mPaperFromPopupMenu.showLocation(mSpLayout, 0, 0);
                break;
        }
    }
}